import ViewUI from 'view-design';
const ERROR_TITLE = `background:#ff0000 ; padding: 3px; border-radius: 3px 0 0 3px;  color: #fff`;
const ERROR_TEXT = `background:#41b883 ; padding: 3px; border-radius: 0 3px 3px 0;  color: #fff`;
const CONSOLE_STYLE = "font-weight: 200;font-style: italic;"
export default {
  install(Vue) {
    const errorHandler = (res) => {
      let { desc, title, output } = {};
      if (res) {
        if (res.message) {
          output = res.message;
          desc = `异常消息：<br />${res.message}<br /><p style="${CONSOLE_STYLE}">详情请查看控制台</p>`;
          title = `脚本异常`
          console.debug(res.stack);
        } else if (res.desc && res.title) {
          output = desc = res.desc;
          title = res.title;
        } else {
          output = res;
          title = res;
        }
      } else {
        title = `未知异常`;
        output = desc = JSON.stringify(res);
      }
      console.debug(`%cERROR%c${output}`, ERROR_TITLE, ERROR_TEXT);
      ViewUI.Notice.error({ title, desc });
      //, duration: 500
    }
    Vue.prototype.$Error = errorHandler;
    Vue.config.errorHandler = errorHandler
  }
}
